﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using WeatherStation;

namespace WeatherStationView
{
    /// <summary>
    /// Interaction logic for HeatIndexDisplay.xaml
    /// </summary>
    public partial class HeatIndexDisplay : UserControl, IObserver
    {
        public HeatIndexDisplay()
        {
            InitializeComponent();
        }

        #region Variables

        private float heatIndex = 0.0f;

        #endregion

        #region IObserver Members

        public void Update(float temperature, float humidity, float pressure)
        {
            heatIndex = computeHeatIndex(temperature, humidity);

            Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new setValuesDelegate(setValues), heatIndex);
        }

        #endregion

        #region WeatherData suscription related

        private Boolean suscribedToWD = false;

        private ISubject wd;

        public ISubject WeatherDataSource
        {
            get
            {
                return wd;
            }
            set
            {
                wd = value;
            }
        }

        #endregion

        #region GUI logic

        private delegate void setValuesDelegate(float heatindex);

        private void setValues(float hi)
        {
            lblHeat.Content = "Heat index is " + hi;
            
        }

        private void btnSuscribeUnsuscribe_Click(object sender, RoutedEventArgs e)
        {
            if (wd == null) return;

            if (suscribedToWD)
            {
                wd.RemoveObserver(this);
                btnSuscribeUnsuscribe.Content = "Suscribe to Weather Data Source";
            }
            else
            {
                wd.RegisterObserver(this);
                btnSuscribeUnsuscribe.Content = "Unsuscribe from Weather Data Source";
            }

            suscribedToWD = !suscribedToWD;
        }

        #endregion

        #region computeHeatIndex
        private float computeHeatIndex(float temperature,
            float relativeHumidity)
        {
            float heatIndex = (float)((16.923 + (0.185212 * temperature) +
                (5.37941 * relativeHumidity) - (0.100254 * temperature * relativeHumidity) +
                (0.00941695 * (temperature * temperature)) +
                (0.00728898 * (relativeHumidity * relativeHumidity)) +
                (0.000345372 * (temperature * temperature * relativeHumidity)) -
                (0.000814971 * (temperature * relativeHumidity * relativeHumidity)) +
                (0.0000102102 * (temperature * temperature * relativeHumidity * relativeHumidity)) -
                (0.000038646 * (temperature * temperature * temperature)) +
                (0.0000291583 * (relativeHumidity * relativeHumidity * relativeHumidity)) +
                (0.00000142721 * (temperature * temperature * temperature * relativeHumidity)) +
                (0.000000197483 * (temperature * relativeHumidity * relativeHumidity * relativeHumidity)) -
                (0.0000000218429 * (temperature * temperature * temperature * relativeHumidity * relativeHumidity)) +
                0.000000000843296 * (temperature * temperature * relativeHumidity * relativeHumidity * relativeHumidity)) -
                (0.0000000000481975 * (temperature * temperature * temperature * relativeHumidity * relativeHumidity * relativeHumidity)));
            return heatIndex;
        }
        #endregion//computeHeatIndex
    }
}
